package cn.hznc.controller.service.order;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.MaterialEntity;
import cn.hznc.domain.request.order.AddOrEditMaterialReq;
import cn.hznc.mapper.MaterialMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author：Administrator
 * @Date：2023/6/11 21:12
 */
@Service
public class MaterialService{

    @Resource
    private MaterialMapper materialMapper;
    public Object addOrEditMaterial(AddOrEditMaterialReq addOrEditMaterialReq) {
        //校验sn号
        LambdaQueryWrapper<MaterialEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaterialEntity::getSn , addOrEditMaterialReq.getSn());
        MaterialEntity materialEntity = materialMapper.selectOne(wrapper);
        if(ObjectUtil.isNull(materialEntity)){
            BeanUtil.copyProperties(addOrEditMaterialReq , materialEntity);
            materialMapper.insert(materialEntity);
        }
        BeanUtil.copyProperties(addOrEditMaterialReq , materialEntity);
        materialMapper.updateById(materialEntity);
        return Boolean.TRUE;
    }

    public Object deleteMaterial(Integer id) {
        QueryWrapper<MaterialEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        materialMapper.delete(wrapper);
        return Boolean.TRUE;
    }
}
